package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.CourseService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Course;
import com.iot08.common.entity.app.Knowledge;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @version 1.0
 * @Description: 课程页
 * @author: Rock
 * @time: 2021/5/31 10:52
 */
@Controller
@RequestMapping("course")
public class CourseController extends BaseController {

    @Resource
    private CourseService courseService;

    /**
     * @param page
     * @time: 2021/5/31 10:54
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<Course> pages = courseService.list(page);
        mv.addObject("courses", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("course/course_list");
        return mv;
    }

    /**
     * @time: 2021/5/31 13:51
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("course/course_add");
        return mv;
    }

    /**
     * @param id
     * @param name
     * @time: 2021/5/31 13:52
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkName")
    public Response<Object> checkName(@RequestParam(required = false) String id, String name) {
        Course course = courseService.getByName(name);
        if (StringUtil.isBlank(id)) {
            if (course != null) {
                return Response.failedResponse("名称已存在");
            }
        } else {
            if (course != null && !id.equals(course.getId())) {
                return Response.failedResponse("名称已存在");
            }
        }
        return Response.successResponse();
    }

    /**
     * @param course
     * @param file
     * @time: 2021/5/31 13:52
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Course course, MultipartFile file) throws IOException {
        ModelAndView mv = new ModelAndView();
        courseService.add(course, this.getSessionUser(), this.getRemortIP(), file);
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id
     * @time: 2021/5/31 13:53
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("course", courseService.findById(id));
        mv.setViewName("course/course_edit");
        return mv;
    }

    /**
     * @param course
     * @param file
     * @time: 2021/5/31 14:22
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(Course course, MultipartFile file) throws IOException {
        ModelAndView mv = new ModelAndView();
        courseService.update(course, this.getSessionUser(), this.getRemortIP(), file);
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id
     * @time: 2021/5/31 14:44
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return courseService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }


}
